const app=getApp();
const { request }  = require('../../request/index.js');
const ImageSource = require('../../utils/image_path.js');
const crypto = require("../../utils/test");
const updateData=require('../../request/dataGet.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        NavbarHeight:app.globalData.navBarHeight + app.globalData.statusBarHeight,
        WindowWidth:app.globalData.windowWidth,
        ScreenHeight:app.globalData.ScreenHeight,
        //openId:app.globalData.openId,
        navigationSetting:{
            title:'我的',
            model:0,
        },
        imgSrc:"",
        userData:[],
        CardNum:[],
        OrderNum:[],
        CoupleNum:[],
        cardsData:[],
        cardsCategory:[],
        aboutViewActive:0,
    },
    onLoad(){

    },
    //预处理数据
    async preDealData(){
        if(app.globalData.userDataChangeFlag==1){
            await updateData.getUserData()
            app.globalData.userDataChangeFlag=0
        }
        if(app.globalData.userCardChangeFlag==1){
            await updateData.getUserCardData()
            app.globalData.userCardChangeFlag=0
        }
        if(app.globalData.userCouplesChangeFlag==1){
            await updateData.getCouples()
            app.globalData.userCouplesChangeFlag=0
        }
        if(app.globalData.userOrdersChangeFlag==1){
            await updateData.getOrders()
            app.globalData.userOrdersChangeFlag=0
        }
        var userData=wx.getStorageSync("userData");
        var userOrders=wx.getStorageSync("userOrders");
        var userCouples=wx.getStorageSync("userCouples");
        var userCard=wx.getStorageSync("userCards");
        var cardCount=0;
        for(let i=0;i<6;i++){
            cardCount+=userCard.data[i].length;
        }
        var CoupleCount=0;
        console.log(userCouples.data)
        for(let i=0;i<userCouples.data.length;i++){
            console.log(userCouples.data[i].message)
            console.log(userCouples.data[i].message.substring(userCouples.data[i].message.length-6))
            if(userCouples.data[i].message.substring(userCouples.data[i].message.length-6)=="unused"){
                CoupleCount++;
            }
        }
        this.setData({
            userData:userData.data,
            OrderNum:userOrders.data.length,
            CoupleNum:CoupleCount,
            CardNum:cardCount,
        })
    },
    getCorrectCardsNum(){
        var num=wx.getStorageSync("cardNum");
        if(!num){
            this.getCardsNum();
        }else{
            if(Date.now()-num.time>1000*10){
                this.getCardsNum();
            }else {
                this.setData({
                    CardNum:num.data
                })
            }
        }
    },
    getCardsNum(){
        var that=this;
        console.log("getCardsData")
        let params={
            url:'/userCard',
            method: 'GET',
            data: {
                openId:app.globalData.openId,
            },
        }
        request(params).then(res=>{
            const data=JSON.parse(crypto.AES_DE(res.data))
            const num=Object.values(data).flat().length
            that.setData({
                CardNum:num
            })
            wx.setStorageSync("cardNum",{time:Date.now(),data:that.data.CardNum})
        })
    },
    onShow(){
        this.preDealData()
    },
    orderItemTap(){
        wx.navigateTo({
            url:'../otherInfo/index?type=0'
        })
    },
    coupleItemTap(){
        wx.navigateTo({
            url:'../otherInfo/index?type=1'
        })
    },
    settingsChangeItemTap(){
        console.log(this.data.userData)
        var obj=JSON.stringify(this.data.userData);
        wx.navigateTo({
            url:'../setting/index?data='+encodeURIComponent(obj),
            //data:this.data.userData
        })
    },
    roleCardItemTap(){
        app.globalData.skipPage=1;
        wx.switchTab({
            url:'../collections/index'
        })
    },
    DiyItemTap(){
        wx.switchTab({
            url:'../diy/index'
        })
    },
    FollowItemTap(e){
        wx.showModal({
            title:"成功关注我们的公众号",
            duration:1500
        })
    },
    AboutMeItemTap(e){
        this.setData({
            aboutViewActive:1,
        })
    },
    closeAboutView(){
       this.setData({
           aboutViewActive:0,
       })
    },
    exitItemTap(e){
        console.log(e)
        wx.exitMiniProgram({success: (res) => {}})
    }
})